﻿using System; 

namespace iwpm.optimization
{
    public class LagrangianHydroInputs
    {
        public string dispatchableUnits;
        public string nondispatchableUnits;
        public double weight_hydro;
        public double[] initialLambdas;
        public double[] initialMus; 
        
        // Construct an instance from a string 
        public static implicit operator LagrangianHydroInputs(string s)
        {
            string[] split = s.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); 
            if (split.Length != 5) 
                throw new Exception("OptiLagrangian inputs are not correctly specified. The string needs to have the following format: \n" + 
                    "dispatchableUnits (unit names sep. by '|')\n" + 
                    "nondispatchableUnits (unit names sep. by '|')\n" + 
                    "weight_hydro (double)\n" + 
                    "initialLambdas (double[])\n" + 
                    "initialMus (double[])");
            LagrangianHydroInputs inputs = new LagrangianHydroInputs();
            inputs.dispatchableUnits = split[0];
            inputs.nondispatchableUnits = split[1];
            inputs.weight_hydro = double.Parse(split[2]);
            inputs.initialLambdas = Array.ConvertAll(split[3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), elem => double.Parse(elem));
            inputs.initialMus = Array.ConvertAll(split[4].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), elem => double.Parse(elem));
            return inputs; 
        }

        // To string 
        public override string ToString()
        {
            return dispatchableUnits + "\n"
                + nondispatchableUnits + "\n"
                + weight_hydro + "\n"
                + string.Join(" ", Array.ConvertAll(initialLambdas, elem => elem.ToString())) + "\n"
                + string.Join(" ", Array.ConvertAll(initialMus, elem => elem.ToString())); 
        }

    }
}
